using System;
using System.Text;
using System.Text.RegularExpressions;

namespace SnippetBox.Core.Common.Extensions {
    public static class StringExtensions {
        public static string Slugify(this string phrase) {
            string str = phrase.RemoveAccent().ToLower();

            str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
            str = Regex.Replace(str, @"\s+", " ").Trim();
            str = Regex.Replace(str, @"\s", "-");

            return str;
        }

        public static string DeSlugify(this string phrase) {
            return phrase.Replace("-", " ");
        }

        public static string RemoveAccent(this string txt) {
            byte[] bytes = Encoding.GetEncoding("Cyrillic").GetBytes(txt);
            return Encoding.ASCII.GetString(bytes);
        }

        public static string AppendLetter(this string value, string character) {
            if (!value.EndsWith(character))
                return value + character;
            return value;
        }

        ///<summary>
        /// Converts carriage returns to HTML new rows.
        ///</summary>
        ///<param name="s"></param>
        ///<returns></returns>
        public static string ToHtmlRows(this string s) {
            return s.Replace(Environment.NewLine, "<br />");
        }

        ///<summary>
        /// See <see cref="string.Format(string,object[])"/>
        ///</summary>
        ///<param name="s"></param>
        ///<param name="args"></param>
        ///<returns></returns>
        public static string FormatWith(this string s, params object[] args) {
            return String.Format(s, args);
        }

        ///<summary>
        /// Removes HTML code from a string
        ///</summary>
        ///<param name="value">The HTML code</param>
        ///<returns></returns>
        public static string RemoveHtml(this string value) {
            if (value == null)
                return null;
            value = value.Replace("<br />", Environment.NewLine);
            const string pattern = @"(?></?\w+)(?>(?:[^>'""]+|'[^']*'|""[^""]*"")*)>";
            return Regex.Replace(value, pattern, String.Empty);
        }


        ///<summary>
        /// Shortens a string at nearest word break from length.
        ///</summary>
        ///<param name="s"></param>
        ///<param name="length">The maximum string length</param>
        ///<returns></returns>
        public static string Shorten(this string s, int length) {
            return Shorten(s, length, "...");
        }

        ///<summary>
        /// Shortens a string at nearest word break from length.
        ///</summary>
        ///<param name="s"></param>
        ///<param name="length">The maximum string length.</param>
        ///<param name="ending">The characters the string should end with.</param>
        ///<returns></returns>
        public static string Shorten(this string s, int length, string ending) {
            if (s.Length > length) {
                string trunc = s.Substring(0, length);
                int indexOfLastSpace = trunc.LastIndexOf(' ');

                if (indexOfLastSpace == -1)
                    return trunc + ending;

                return trunc.Substring(0, indexOfLastSpace) + ending;
            }
            return s;
        }

        ///<summary>
        /// Wrapper for <see cref="string.IsNullOrEmpty"/>
        ///</summary>
        ///<param name="value"></param>
        ///<returns></returns>
        public static bool IsNullOrEmpty(this string value) {
            return String.IsNullOrWhiteSpace(value);
        }
    }
}